const db = require('../db/index.js')
exports.getMenuInfo = (req, res) => {
    let sql = 'SELECT a.ps_id,a.ps_name,a.ps_pid,a.ps_level,b.ps_api_path FROM sp_permission a JOIN sp_permission_api b  ON(a.ps_id = b.ps_id)'
    db.query(sql, function(err, results) {
            var obj = {
                data: '',
                meta: {
                    status: '',
                    msg: ''
                }
            }
            if (err) {
                obj.meta.status = 500;
                obj.meta.msg = "数据库查询错误";
                return res.send(obj);
            } else {
                let arr = [];
                for (var i = 0; i < results.length; i++) {
                    if (results[i]['ps_level'] == 0) { //表示一级菜单
                        results[i]['children'] = []; //定义一个存储二级菜单的地方
                        arr.push({...results[i], path: results[i].ps_api_path, id: results[i].ps_id, authName: results[i].ps_name }); //将一级菜单存储
                    }
                }
                for (var i = 0; i < results.length; i++) {
                    if (results[i]['ps_level'] == 1) { //表示二级菜单
                        for (var j = 0; j < arr.length; j++) {
                            if (results[i].ps_pid == arr[j].ps_id) {
                                results[i].path = results[i].ps_api_path;
                                results[i].id = results[i].ps_id;
                                results[i].authName = results[i].ps_name;
                                arr[j].children.push(results[i]);
                            }
                        }
                    }
                }
                obj.meta.status = 200;
                obj.meta.msg = "获取菜单列表成功";
                obj.data = arr;
                return res.send(obj)
            }
        })
        // res.send('ok');
}